package org.yoao.crm.struts.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.yoao.crm.struts.form.MmuserForm;
import org.yoao.moneymachine.business.impl.UserServiceImpl;
import org.yoao.moneymachine.business.service.IUserService;

/**
 * @author yxwonline.com
 * @description 
 * @date 2010-8-4 上午12:18:56
 */
public class MMRegeditServlet extends HttpServlet {

	public MMRegeditServlet() {
		super();
	}

	@Override
	public void init() throws ServletException {
		
	}
	
	@Override
	public void destroy() {
		super.destroy();		
	}

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}
	
	private IUserService userService = new UserServiceImpl();
	
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String returnUrl = null;
		try {
			String method = request.getParameter("method");
			if("regedit".equals(method)){
				response.setCharacterEncoding("GBK");
				PrintWriter out = response.getWriter();
				String result = null;
				returnUrl = request.getParameter("rurl");;
				String info = regedit(request);
				if(info.indexOf("成功") > -1)
					result = "<script>alert('注册成功!');history.back();</script>";
				else if(info.indexOf("失败") > -1)
					result = "<script>alert('注册失败!');history.back();</script>";
				else
					result = "<script>alert('"+info+"!');history.back();</script>";
				out.println(result);
				out.flush();
				out.close();
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		response.sendRedirect(returnUrl);
	}
	
	private String regedit(HttpServletRequest request){		
		try {
			String userName = request.getParameter("userName");
			String passWord = request.getParameter("passWord");
			String repassWord = request.getParameter("repassWord");
			String mobile = request.getParameter("mobile");
			String email = request.getParameter("email");
			String qq = request.getParameter("qq");
			String userCode = request.getParameter("expandCode");
			
			if(repassWord == null || repassWord.trim().equals("")){
				return "未输入密码!";
			}
			if(repassWord == null || repassWord.trim().equals("")){
				return "未输入确认密码!";
			}
			if(!repassWord.trim().equals(passWord)){
				return "两次密码输入不一致!";
			}
			
			MmuserForm userForm = new MmuserForm();
			userForm.setUserName(userName);
			userForm.setPassWord(passWord);
			userForm.setMobile(mobile);
			userForm.setEmail(email);
			userForm.setQq(qq);
			userForm.setUserCode(userCode);
			return userService.regedit(userForm);
		} catch (Exception e) {
			e.printStackTrace();
			return "注册失败";
		}
	}
}
